package com.pactera.asmp.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pactera.asmp.server.common.constants.ResultCode;
import com.pactera.asmp.server.common.exception.CustomException;
import com.pactera.asmp.server.dao.ScriptGroupMapper;
import com.pactera.asmp.server.dao.ScriptInfoMapper;
import com.pactera.asmp.server.entity.ScriptGroupSearchDTO;
import com.pactera.asmp.server.pojo.ScriptGroupInfo;
import com.pactera.asmp.server.pojo.ScriptInfo;
import com.pactera.asmp.server.pojo.SelectScriptGroupInfoResult;
import com.pactera.asmp.server.pojo.User;
import com.pactera.asmp.server.utils.PageDataResult;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName: asmp-server
 * @Package com.pactera.asmp.server.service
 * @ClassName: ScriptInfoServcieImpl
 * @Description: ScriptInfoServcieImpl
 * @Date 2020/07/24 16:34
 * @Author lvzhiqiang
 * @Version 1.0
 */
@Service
public class ScriptGroupServcieImpl implements IScriptGroupServcie {

    @Autowired
    private ScriptGroupMapper mScriptGroupMapper;
    @Autowired
    private ScriptInfoMapper mScriptInfoMapper;

    @Override
    public Integer insert(ScriptGroupInfo scriptGroupInfo) {
        User currUser = (User) SecurityUtils.getSubject().getPrincipal();
        if (currUser == null) throw new CustomException(ResultCode.PERMISSION_EXPIRE);
        scriptGroupInfo.setUserId(currUser.getId());
        scriptGroupInfo.setTimeCreate(new Date());

        boolean result = mScriptGroupMapper.insert(scriptGroupInfo) > 0;
        if(result){
          throw  new CustomException(ResultCode.INTERFACE_OPERATION_INVOKE_ERROR);
        }
        return scriptGroupInfo.getId();
    }


    @Override
    public boolean update(ScriptGroupInfo scriptGroupInfo) {
        return mScriptGroupMapper.update(scriptGroupInfo) > 0 ;
    }

    @Override
    public PageDataResult select(ScriptGroupSearchDTO scriptGroupSearchDTO) {
        PageDataResult pageDataResult = new PageDataResult();
        PageHelper.startPage(scriptGroupSearchDTO.getPage(), scriptGroupSearchDTO.getLimit());
        List<ScriptGroupInfo> list = mScriptGroupMapper.select(scriptGroupSearchDTO);
        List<SelectScriptGroupInfoResult> results = new ArrayList<>();
        for(ScriptGroupInfo groupInfo: list){
            List<ScriptInfo> scriptInfos = mScriptInfoMapper.selectScriptByGroupId(groupInfo.getId());
            SelectScriptGroupInfoResult item = new SelectScriptGroupInfoResult();
            item.setId(groupInfo.getId());
            item.setName(groupInfo.getName());
            item.setDescription(groupInfo.getDescription());
            item.setParentId(groupInfo.getParentId());
            item.setProjectId(groupInfo.getProjectId());
            item.setUserId(groupInfo.getUserId());
            item.setUserName(groupInfo.getUserName());
            item.setData(scriptInfos);
            results.add(item);
        }

        PageInfo<SelectScriptGroupInfoResult> pageInfo = new PageInfo<>(results);
        pageDataResult.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
        pageDataResult.setList(results);
        return pageDataResult;
    }


    @Override
    public PageDataResult selectScriptGroupInfo(ScriptGroupSearchDTO scriptGroupSearchDTO) {
        PageDataResult pageDataResult = new PageDataResult();
        PageHelper.startPage(scriptGroupSearchDTO.getPage(), scriptGroupSearchDTO.getLimit());
        List<ScriptGroupInfo> list = mScriptGroupMapper.select(scriptGroupSearchDTO);
        PageInfo<ScriptGroupInfo> pageInfo = new PageInfo<>(list);
        pageDataResult.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
        pageDataResult.setList(list);
        return pageDataResult;
    }


    @Override
    public boolean deleteGroupAndScriptRelByGID(List<Integer> groupIds) {
        mScriptGroupMapper.deleteGroupAndScriptRelByGID(groupIds);
        return true;
    }

    @Override
    public boolean deleteInGroupThisScriptRel(Integer groupId, List<Integer> scriptIds) {
        mScriptGroupMapper.deleteInGroupThisScriptRel(groupId, scriptIds);
        return true;
    }

}
